﻿(function ($) {
    $.fn.checkbox = function (options) {

        var $settings = $.extend({
            'useInternalStyle': true,
            'chooseText': 'Wybierz',
            'cancelText': 'Anuluj',
            'labelWidth': 0
        }, options);


        if ($settings['useInternalStyle']) {
            if (!$('head #checkbox-style').is('style')) {
                $('<style type="text/css" id="checkbox-style">' + checkboxStyle + '</style>').prependTo($('head'));
            }
        }

        return this.each(function () {
            var $this = $(this);
            var type = $this.attr('type');
            var $label = $('label[for=' + $this.attr('id') + ']');
            var $el = $('<div class="bh-checkbox">\
    <div class="bh-checkbox-bntwrap">\
        <a class="check">'+ $settings['chooseText'] + '</a>\
        <a class="uncheck">' + $settings['cancelText'] + '</a>\
    </div>\
    <div class="bh-checkbox-label">' + $label.html() + '</div>\
    <div class="clear"></div>\
</div>\
');
            var initState = $this.is('input:checked') && $this.attr('checked');
            $el.addClass(initState ? 'bh-checkbox-checked' : 'bh-checkbox-unchecked');
            $this.hide();
            $label.hide();
            $this.after($el);

            if ($settings['labelWidth'] > 0) {
                $el.find('.bh-checkbox-label').width($settings['labelWidth']);
            }

            $el.attr('name', $this.attr('name') + '-wrap');
            $el.data('input', $this);
            $el.data('setCheck', function ($input, $el, toState) {
                if (toState) {
                    $input.attr('checked', true);
                    $el.addClass('bh-checkbox-checked');
                    $el.removeClass('bh-checkbox-unchecked');
                } else {
                    $input.attr('checked', false);
                    $el.removeClass('bh-checkbox-checked');
                    $el.addClass('bh-checkbox-unchecked');
                }
                $input.trigger('change');
            });
            $el.click(function (e) {
                var $input = $(this).data('input');
                var state = $input.is('input:checked') && $input.attr('checked');
                if (state) {
                    $(this).data('setCheck')($input, $el, false);
                } else {
                    if ($input.attr('type') == 'radio') {
                        $('.bh-checkbox-checked[name=' + $input.attr('name').replace(/\$/g, '\\$') + '-wrap]').each(function () {
                            $(this).data('setCheck')($(this).data('input'), $(this), false);
                        });
                    }
                    $(this).data('setCheck')($input, $el, true);
                }
            });
        });

    }
})(jQuery);

var checkboxStyle = ' \
 .bh-checkbox { \
    display: inline-block; \
    background-color: #eee; \
    color: #404040; \
    -webkit-border-radius: 10px; \
    border-radius: 10px; \
    font-size: 11px; \
    } \
    .bh-checkbox-bntwrap { \
        float: left; \
    } \
    .bh-checkbox-bntwrap { \
        margin-right: 10px; \
        padding: 10px; \
        border-right: solid 1px #610C00; \
    } \
    .bh-checkbox-bntwrap a { \
        -webkit-border-radius: 5px; \
        border-radius: 5px; \
        background-color: #91a123; \
        color: #fff; \
        font-weight: bold; \
        padding: 7px 10px; \
        display: block; \
        cursor: pointer; \
    } \
    .bh-checkbox-label { \
        float: right; \
        padding: 10px; \
        text-align: center; \
    } \
    .bh-checkbox-unchecked .uncheck { \
        display: none; \
    } \
    .bh-checkbox-checked .check { \
        display: none; \
    } \
    .bh-checkbox-checked .uncheck { \
        background-color: transparent; \
        padding: 7px 0 7px 30px; \
    } \
    .bh-checkbox-checked { \
        background-color: #91A123; \
        background-image: url(\'/data/sites/1/skins/blackhouse/images/checkbox-checked.jpg\'); \
        background-repeat: no-repeat;\
        background-position: 7px 10px;\
    } \
    .bh-checkbox-checked .bh-checkbox-label { \
       font-weight:bold;\
    }\
';

$(function () {
    $('input.clear-default').click(function () {
        if ($(this).val() == this.defaultValue)
            $(this).val('');
    });


    $('.addserv-line .clicker').click(function () {
        if ($(this).parent().hasClass('addserv-activeline'))
            $(this).parent().removeClass('addserv-activeline');
        else {
            $('.addserv-line').removeClass('addserv-activeline');
            $(this).parent().addClass('addserv-activeline');
            $(this).parent().find('.addserv-roll').hide();
            $(this).parent().find('.addserv-price').show();
        }
    });
    $('#txtArrivalTime').keyup(function () {
        var v = $(this).val().split(':');
        if (v.length == 2) {
            v = parseInt(v[0]);
            if (v < 14) {
                $('#lblNextDayArrival').show();
                return;
            }
        }
        $('#lblNextDayArrival').hide();
    });
});











function PriceTranslate() {

    this.ValuesToTranslate = new Array();

    this.Add = function (domElement, value) {
        if (!window.TranslatedValues)
            window.TranslatedValues = new Array();
        if (window.TranslatedValues[value]) {
            $(domElement).text(window.TranslatedValues[value]);
            return;
        }
        $(domElement).text(value.toFixed(2));
        this.ValuesToTranslate.push({
            Value: value,
            Dom: domElement
        });
    }

    this.Translate = function () {
        if (this.ValuesToTranslate.length == 0) return;
        var sameValues = new Array();
        for (var xv = 0; xv < this.ValuesToTranslate.length; xv++) {
            sameValues[xv] = this.ValuesToTranslate[xv].Value;
        }
        $.ajax({
            type: "POST",
            url: "/Handlers/BlackApartService.asmx/CurrencyToString",
            data: '{ values:' + JSON.stringify(sameValues) + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            complex: this.ValuesToTranslate,
            success: function (msg) {
                for (var xc = 0; xc < this.complex.length; xc++) {
                    $(this.complex[xc].Dom).text(msg['d'][xc]);
                    window.TranslatedValues[this.complex[xc].Value] = msg['d'][xc];
                }
            }
        });
        this.ValuesToTranslate = new Array();
    }

}

(function ($) {

    var methods = {
        init: function (options) {
            if (!this.data('serviceBlock')) {
                var d = {
                    unitPrice: parseFloat(this.find('input[id$=_unitprice]').val()),
                    quantityForAll: parseInt(this.find('input[id$=_quanntityForAll]').val()),
                    apartmentCount: parseInt(this.find('input[id$=_apartmentcount]').val()),
                    maxPersons: parseInt(this.find('input[id$=_maxPersons]').val()),
                    DOM: {
                        Quantity: this.find('.quantity'),
                        Price: this.find('.addserv-price'),
                        Review: this.find('.adv-review'),
                        Block: this
                    }
                };
                if (!d.unitPrice) d.unitPrice = 0;
                if (!d.quantityForAll) d.quantityForAll = 1;
                if (!d.apartmentCount) d.apartmentCount = 1;
                if (!d.maxPersons) d.maxPersons = 100;
                this.data('serviceBlock', d);
            }
        },
        setQuantity: function (quantity) {
            var d = this.data('serviceBlock');
            quantity = quantity * d.apartmentCount;
            d.DOM.Quantity.text(quantity);
            var translate = new PriceTranslate();
            d.DOM.Price.data('price', quantity * d.unitPrice);
            translate.Add(d.DOM.Price, quantity * d.unitPrice);

            var allPrice = 0;
            $('.addserv-line').each(function () {
                allPrice = allPrice + $(this).serviceBlock('getPrice');
            });

            translate.Add($('#additionalServicesCost'), allPrice);
            translate.Add($('#totalCost'), allPrice + parseFloat($('#reservationCost').val()));
            translate.Translate();

            if (quantity == 0)
                d.DOM.Review.hide();
            else
                d.DOM.Review.show();
            return this;
        },
        setQuantityForAll: function () {
            var d = this.data('serviceBlock');
            return methods['setQuantity'].apply(this, [d.quantityForAll]);
        },
        getPrice: function () {
            var d = this.data('serviceBlock');
            price = parseFloat(d.DOM.Price.data('price'));
            if (!price)
                return 0;
            else
                return price;
        }
    };

    $.fn.serviceBlock = function (method) {

        // Method calling logic
        if (methods[method]) {
            methods.init.apply(this, arguments);
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.serviceBlock');
        }

    };

})(jQuery);

$(function () {

    $('.py-chb[clientui]').each(function () {
        $(this).find('input').attr('clientui', $(this).attr('clientui')).checkbox({ chooseText: 'Zamawiam' });
    });


    $('*[id$=selectedVariant] input[value=forAll]').change(function () {
        if ($(this).is(':checked'))
            $('#' + $(this).attr('clientui')).serviceBlock('setQuantityForAll');
        else
            $('#' + $(this).attr('clientui')).serviceBlock('setQuantity', 0);
    });
    $('*[id$=selectedVariant] input[value=perDay]').change(function () {
        var $clientUI = $('#' + $(this).attr('clientui'));
        if ($(this).is(':checked')) {
            $clientUI.serviceBlock('setQuantity', $clientUI.find('.checkin-dates input:checked').length);
            $clientUI.find('.checkin-dates').show();
        } else {
            $clientUI.find('.checkin-dates').hide();
            $clientUI.serviceBlock('setQuantity', 0);
        }
    });
    $('*[id$=selectedVariant] input[value=perDay]').each(function () {
        var $option = $(this);
        var $clientUI = $('#' + $(this).attr('clientui'));
        $clientUI.find('.checkin-dates input').keypress(function (event) {
            if (event.keyCode != 13) {
                var i = $(this).data('time');
                if (i)
                    window.clearTimeout(i);
                var s = '$(\'#' + $(this).attr('id') + '\').trigger(\'change\');';
                $(this).data('time', window.setTimeout(s, 500));
            }
        }).change(function () {
            if ($(this).attr('type') == 'checkbox')
                $clientUI.serviceBlock('setQuantity', $clientUI.find('.checkin-dates input:checked').length);
            else {
                var qu = 0;
                var i = $(this).data('time');
                if (i)
                    window.clearTimeout(i);
                $clientUI.find('.checkin-dates tbody input:text').each(function () {
                    var v = parseInt($(this).val());
                    if (v)
                        qu = qu + v;
                });
                $clientUI.serviceBlock('setQuantity', qu);
            }
        });
        var $mp = $clientUI.find('input[id$=_maxPersons]');
        if ($mp.is('input')) {
            var mp = parseInt($mp.val());
            $clientUI.find('.checkin-dates input:text').keyfilter(function (c) {
                var v = parseInt($(this).val() + c);
                return v >= 0 && v <= mp;
            });
        }
    });
    $('.addserv-hold-quantity input').click(function () {
        $(this).data('prevValue', $(this).val());
        $(this).val('');
    }).blur(function () {
        if ($(this).val() == '' && $(this).data('prevValue'))
            $(this).val($(this).data('prevValue'));
    });
    $('.checkin-dates').each(function () {
        var $tab = $(this);
        $tab.find('.checkin-action input').change(function () {
            $tab.find('tbody input').val($(this).val());
            $tab.find('tbody input:eq(0)').trigger('change');
        })
    });


    $('input[id$=_chbOrder]').change(function () {
        if ($(this).is(':checked'))
            $('#' + $(this).attr('clientui')).serviceBlock('setQuantity', 1);
        else
            $('#' + $(this).attr('clientui')).serviceBlock('setQuantity', 0);
    });
});

$(function () {
    $('.checkin-dates tbody td').each(function () {
        var $td = $(this);
        $td.find('a.check').click(function (e) {
            e.preventDefault();
            $td.addClass('selected');
            $td.find('input[type=checkbox]').attr('checked', true).trigger('change');
        });
        $td.find('a.uncheck').click(function (e) {
            e.preventDefault();
            $td.removeClass('selected');
            $td.find('input[type=checkbox]').attr('checked', false).trigger('change');
        });
    });
});

$(function () {
    $('.payment-types li').each(function () {
        var $li = $(this);
        var h = $li.height();
        $li.find('.payment-rabat').css('line-height', h + 'px').height(h);
        $li.find('.payment-fullline').css('line-height', h + 'px').height(h);
    }).css('cursor', 'pointer').click(function () {
        var $li = $(this);
        $('.payment-types li').removeClass('selected');
        $li.addClass('selected');
        $li.find('input').attr('checked', true);

        $('#paymentSummary-rabatValue').text($li.find('#rabatPriceValue').val());
        $('#paymentSummary-totalCost').text($li.find('#reservationValueWithRabat').val());
        $('#rabat-value').text($li.find('.payment-rabat-value').text());
    });
    $('.pcheck-check').each(function () {
        var oh = $(this).outerHeight();
        var hp = $(this).parent().height();
        var tm = Math.round((hp - oh) / 2);
        $(this).css('margin-top', tm + 'px');
    });
    $('.payment-types li:first').addClass('selected').find('input').attr('checked', true);
});